package com.distribute.application.api.rest.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.Agent;
import com.distribute.framework.model.Commodity;
import com.distribute.framework.model.Commodity.ZonePrice;
import com.distribute.framework.model.Coupon;
import com.distribute.framework.model.DirectOrder;
import com.distribute.framework.model.Higher;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.Merchant;
import com.distribute.framework.model.OrderDetails;
import com.distribute.framework.model.Parameter;
import com.distribute.framework.model.Product;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.ShoppingCart;
import com.distribute.framework.model.TbPartners;

@RestController(value="api.MemberShopController")
public class MemberShopController  extends BaseApiController{
	/**
	 * 会员根据品牌获取商品列表
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/goodsLists.do")
	public @title("会员根据品牌获取商品列表")  Protocol goodsLists(
		@title("页码") Integer pageNo
		,@title("个数") Integer pageSize
		,@title("所属品牌") Long merchant
		,@title("城市") String city
		,@title("省份") String province
	){
		Protocol protocol = Protocol.create(true, null);
		Region region=Region.where.name(province).level((short)1).get();
		 Long citys=null;
		 Long proviceIds=null;
		if (region!=null) {
			proviceIds=region.getId();
			Region region2=Region.where.parent(region.getId()).name(city).level((short)2).get();
			if (region2!=null) {
				citys=region2.getId();
			}
		}
		final Long cityId=citys;
		final Long proviceId=proviceIds;
		Commodity.M where = Commodity.where.merchant(merchant).status((short)1);
		protocol.setPage(where.page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<Commodity>(){
			@Override
			public Map format(Commodity t) {
				Double price=0.0;
				if (cityId!=null||proviceId!=null) {
					List<ZonePrice> zonePrices=t.get$zonePrice();
					if (zonePrices.size()>0) {
						for (ZonePrice zonePrice : zonePrices) {
							if (zonePrice!=null) {
								if (zonePrice.getArea()!=null&&!zonePrice.getArea().equals("")&&cityId!=null&&proviceId!=null) {
									if (zonePrice.getArea().equals(cityId.toString())||zonePrice.getArea().equals(proviceId.toString())) {
										price=Double.valueOf(zonePrice.getPrice());
									}
								}
							}
						}
					}
				}
				if (price>0) {
				   t.setPrice(price);
				}
				Map json = t.toJSON();
				return json;
			}
		});
		
		return protocol;
	}
	
		/**
		 * 删除购物车会员
		 * @param banrdId
		 * @return
		 */
		@RequestMapping("/api/memberShop/deletCar.do")
		public @title("删除购物车会员")  Protocol deletCar(
			@title("编号") Long id
		){
			Protocol protocol = Protocol.create(true, null);
			ShoppingCart shoppingCart=ShoppingCart.where.id(id).get();
			if (shoppingCart!=null) {
				shoppingCart.delete();
				protocol.set("type", true);
			}else {
				protocol.set("type", false);
			}
			return protocol;
		}
		/**
		 * 获取商品详情也系统参数
		 * @param banrdId
		 * @return
		 */
		@RequestMapping("/api/memberShop/getDetailParams.do")
		public @title("获取商品详情也系统参数")  Protocol getDetailParams(
			@title("品牌") Long banrdId
		){
			Protocol protocol = Protocol.create(true, null);
			Merchant merchant=Merchant.where.id(banrdId).get();
			Parameter parameter=Parameter.where.merchant(merchant.getId()).get();
			protocol.set("merchant", merchant);
			protocol.set("pam", parameter);
			return protocol;
		}
	/**
	 * 会员根据品牌获取充值页商品列表
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/chongGoodsLists.do")
	public @title("会员根据品牌获取充值页商品列表")  Protocol chongGoodsLists(
		@title("所属品牌") Long merchant
		,@title("城市") String city
		,@title("省份") String province
	){
		Protocol protocol = Protocol.create(true, null);
		Region region=Region.where.name(province).level((short)1).get();
		 Long citys=null;
		if (region!=null) {
			Region region2=Region.where.parent(region.getId()).name(city).level((short)2).get();
			if (region2!=null) {
				citys=region2.getId();
			}
		}
		final Long cityId=citys;
		Commodity.M where = Commodity.where.merchant(merchant).status((short)1);
		protocol.setPage(where.page(0, 200), new Protocol.BeanFormat<Commodity>(){
			@Override
			public Map format(Commodity t) {
				Double price=0.0;
				if (cityId!=null) {
					List<ZonePrice> zonePrices=t.get$zonePrice();
					if (zonePrices.size()>0) {
						for (ZonePrice zonePrice : zonePrices) {
							if (zonePrice.getArea().equals(cityId.toString())) {
								price=Double.valueOf(zonePrice.getPrice());
							}
						}
					}
				}
				if (price>0) {
				   t.setPrice(price);
				}
				Map json = t.toJSON();
				return json;
			}
		});
		
		return protocol;
	}
	/**
	 * 用户获取单个商品详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/goodsDetail.do")
	public @title("用户获取单个商品详情") Protocol goodsDetail(@title("商品信息ID*") Long id
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Commodity commodity = Commodity.where.id(id).get();
		if(commodity!=null){
			TbPartners agentb=TbPartners.where.tbpMember(member.getId()).tbpBrand(commodity.getMerchant()).tbpPayCard((short)2).get();
            Agent agent=Agent.where.member(member.getId()).merchant(commodity.getMerchant()).status((short)1).get();			
			protocol.set("agentType", agentb!=null?3:agent!=null?2:1);//普通分销员
			protocol.set("entity", commodity.toJSON());
		}else {
			return Protocol.create(false, "商品已下架","goods_not_found");
		}
		List<Higher> highers=Higher.where.commodity(commodity.getId()).list(5);
		protocol.set("hig", highers);
		
		return protocol;
	}
	
	/**
	 * 用户加入购物车
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/jiaRuCar.do")
	public @title("用户加入购物车") Protocol jiaRuCar(
			@title("商品信息ID") Long id,
			@title("价格") Double price,
			@title("商品规格属性") String atrebuite,
			@title("商品数量") Integer count
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Commodity commodity=Commodity.where.id(id).get();
		ShoppingCart shoppingCartsCart=ShoppingCart.where.member(member.getId()).brand(commodity.getMerchant()).goods(commodity.getId()).get();
		if (shoppingCartsCart!=null) {
			if (atrebuite.equals(shoppingCartsCart.getAttribute())) {
				shoppingCartsCart.setCount(shoppingCartsCart.getCount()+count);
				shoppingCartsCart.update();
			}else {
				ShoppingCart shoppingCart=new ShoppingCart(
						commodity.getId()//商品信息
						,commodity.getTitle()
					 	,price//价格
					 	,count//数量
					 	,commodity.getImage()//图片
					 	,commodity.getHowMuch()//充
					 	,commodity.getHowMany()//赠
					 	,member.getId()//用户信息
					 	,atrebuite//属性
					 	,commodity.getMerchant()//品牌
					 	,"0"//选择
						);
				shoppingCart.save();
			}
		}else {
			ShoppingCart shoppingCart=new ShoppingCart(
					commodity.getId()//商品信息
					,commodity.getTitle()
				 	,price//价格
				 	,count//数量
				 	,commodity.getImage()//图片
				 	,commodity.getHowMuch()//充
				 	,commodity.getHowMany()//赠
				 	,member.getId()//用户信息
				 	,atrebuite//属性
				 	,commodity.getMerchant()//品牌
				 	,"0"//选择
					);
			shoppingCart.save();
		}
		return protocol;
	}
	/**
	 * 用户获取购物车列表
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/carList.do")
	public @title("用户获取购物车列表") Protocol carList(
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize,
			@title("品牌编号") Long banrdId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(ShoppingCart.where.member(member.getId()).brand(banrdId).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<ShoppingCart>(){
			@Override
			public Map format(ShoppingCart t) {
				Map json = t.toJSON();
				return json;
			}
		});

		return protocol;
	}
	/**
	 * 用户获取订单可用余额
	 * @param id
	 * @return
	 */
	@RequestMapping("/api/memberShop/orderProducsList.do")
	public @title("用户获取订单可用余额") Protocol orderProducsList(
			@title("订单编号") Long orderId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		DirectOrder directOrder=DirectOrder.where.id(orderId).get();
		List<OrderDetails> orderDetails=OrderDetails.where.orders(directOrder.getId()).list(100);
        List<Product> coupons=new ArrayList<Product>();
		for (OrderDetails orderDetails2 : orderDetails) {
			 Product product=Product.where.member(member.getId()).merchant(directOrder.getMerchant()).commodity(orderDetails2.getGoodsNumber()).get();
				if (product!=null&&product.getNumber()>0) {
					product.setpLimit(orderDetails2.getNumber());
				   coupons.add(product);
				}
		}
		protocol.setList(coupons, new Protocol.BeanFormat<Product>(){
			@Override
			public Map format(Product t) {
				Map<String, Object> json=t.toJSON();
				Commodity commodity=Commodity.where.id(t.getCommodity()).get();
				json.put("price", commodity.getPrice());
				json.put("count", 0);
				json.put("select", 0);
				return json;
			}
		});
		return protocol;
	}
}
